package com.example.administrator.jokeyboarddemo.keyboard;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.administrator.jokeyboarddemo.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by gongshuobo on 2016/11/18.
 * 数学键盘View
 * 主要包括五种数学小键盘之间的切换
 */

public class KeyBoardMathView extends LinearLayout {

    /**
     * 数学键盘五种布局(数字,单位,角度,字母,数学符号)
     */
    private View mathCmView;
    private View mathCornerView;
    private View mathNumberView;
    private View mathDivisionView;
    private View mathLetterSmallView;
    private View mathLetterCaseView;

    /**
     * 数学键盘对应的Map键映射
     **/
    private Map<Integer, String> keyboardMathNumberMap;

    /**
     * 数学键盘实现输入完成接口
     **/
    private KeyBoardCallBack keyBoardCallBack;

    /**
     * 数学键盘乘除对应的id和值映射
     **/
    private int[] mathDivisionKey = {R.id.tv_math_division_more, R.id.tv_math_division_less, R.id.tv_math_division_no_equal, R.id.tv_math_division_more_equal, R.id.tv_math_division_less_equal, R.id.tv_math_division_left_bracket, R.id.tv_math_division_right_bracket, R.id.tv_math_division_left_center_bracket, R.id.tv_math_division_right_center_bracket, R.id.tv_math_division_left_brace, R.id.tv_math_division_right_brace, R.id.tv_math_division_zero_power, R.id.tv_math_division_two_power, R.id.tv_math_division_three_power, R.id.tv_math_division_thousand};
    private int[] mathDivisionValue = {R.string.keyboard_sign_more, R.string.keyboard_sign_less, R.string.keyboard_sign_no_equal, R.string.keyboard_sign_more_equal, R.string.keyboard_sign_less_equal, R.string.keyboard_sign_left_bracket, R.string.keyboard_sign_right_bracket, R.string.keyboard_sing_left_center_bracket, R.string.keyboard_sign_right_center_bracket, R.string.keyboard_sign_left_brace, R.string.keyboard_sign_right_brace, R.string.keyboard_sign_zero_power, R.string.keyboard_sign_two_power, R.string.keyboard_sign_three_power, R.string.keyboard_sign_thousand};

    /**
     * 数学键盘小写字母对应的id和值映射
     **/
    private int[] mathLetterSmallKey = {R.id.tv_math_letter_a, R.id.tv_math_letter_b, R.id.tv_math_letter_c, R.id.tv_math_letter_d, R.id.tv_math_letter_e, R.id.tv_math_letter_f, R.id.tv_math_letter_g, R.id.tv_math_letter_h, R.id.tv_math_letter_i, R.id.tv_math_letter_j, R.id.tv_math_letter_k, R.id.tv_math_letter_l, R.id.tv_math_letter_m, R.id.tv_math_letter_n, R.id.tv_math_letter_o, R.id.tv_math_letter_p, R.id.tv_math_letter_q, R.id.tv_math_letter_r, R.id.tv_math_letter_s, R.id.tv_math_letter_t, R.id.tv_math_letter_u, R.id.tv_math_letter_v, R.id.tv_math_letter_w, R.id.tv_math_letter_x, R.id.tv_math_letter_y, R.id.tv_math_letter_z};
    private int[] mathLetterSmallValue = {R.string.keyboard_sign_a, R.string.keyboard_sign_b, R.string.keyboard_sign_c, R.string.keyboard_sign_d, R.string.keyboard_sign_e, R.string.keyboard_sign_f, R.string.keyboard_sign_g, R.string.keyboard_sign_h, R.string.keyboard_sign_i, R.string.keyboard_sign_j, R.string.keyboard_sign_k, R.string.keyboard_sign_l, R.string.keyboard_sign_m, R.string.keyboard_sign_n, R.string.keyboard_sign_o, R.string.keyboard_sign_p, R.string.keyboard_sign_q, R.string.keyboard_sign_r, R.string.keyboard_sign_s, R.string.keyboard_sign_t, R.string.keyboard_sign_u, R.string.keyboard_sign_v, R.string.keyboard_sign_w, R.string.keyboard_sign_x, R.string.keyboard_sign_y, R.string.keyboard_sign_z};
    /**
     * 数学键盘大写字母对应的id和值映射
     **/
    private int[] mathLetterCaseKey = {R.id.tv_math_letter_A, R.id.tv_math_letter_B, R.id.tv_math_letter_C, R.id.tv_math_letter_D, R.id.tv_math_letter_E, R.id.tv_math_letter_F, R.id.tv_math_letter_G, R.id.tv_math_letter_H, R.id.tv_math_letter_I, R.id.tv_math_letter_J, R.id.tv_math_letter_K, R.id.tv_math_letter_L, R.id.tv_math_letter_M, R.id.tv_math_letter_N, R.id.tv_math_letter_O, R.id.tv_math_letter_P, R.id.tv_math_letter_Q, R.id.tv_math_letter_R, R.id.tv_math_letter_S, R.id.tv_math_letter_T, R.id.tv_math_letter_U, R.id.tv_math_letter_V, R.id.tv_math_letter_W, R.id.tv_math_letter_X, R.id.tv_math_letter_Y, R.id.tv_math_letter_Z};
    private int[] mathLetterCaseValue = {R.string.keyboard_sign_capital_a, R.string.keyboard_sign_capital_b, R.string.keyboard_sign_capital_c, R.string.keyboard_sign_capital_d, R.string.keyboard_sign_capital_e, R.string.keyboard_sign_capital_f, R.string.keyboard_sign_capital_g, R.string.keyboard_sign_capital_h, R.string.keyboard_sign_capital_i, R.string.keyboard_sign_capital_j, R.string.keyboard_sign_capital_k, R.string.keyboard_sign_capital_l, R.string.keyboard_sign_capital_m, R.string.keyboard_sign_capital_n, R.string.keyboard_sign_capital_o, R.string.keyboard_sign_capital_p, R.string.keyboard_sign_capital_q, R.string.keyboard_sign_capital_r, R.string.keyboard_sign_capital_s, R.string.keyboard_sign_capital_t, R.string.keyboard_sign_capital_u, R.string.keyboard_sign_capital_v, R.string.keyboard_sign_capital_w, R.string.keyboard_sign_capital_x, R.string.keyboard_sign_capital_y, R.string.keyboard_sign_capital_z};
    /**
     * 数学键盘公共按键对应的id和值映射,同时是数字键盘的值映射
     **/
    private int[] mathCommonKey = {R.id.tv_number_one, R.id.tv_number_two, R.id.tv_number_three, R.id.tv_number_four, R.id.tv_number_five, R.id.tv_number_sex, R.id.tv_number_seven, R.id.tv_number_eight, R.id.tv_number_nine, R.id.tv_number_zero, R.id.tv_math_number_addition, R.id.tv_number_subtraction, R.id.tv_number_multiplication, R.id.tv_number_division, R.id.tv_number_equal, R.id.tv_math_number_percentage, R.id.tv_math_number_point, R.id.tv_math_number_colon, R.id.tv_math_number_ellipsis, R.id.tv_math_number_approximate, R.id.tv_math_number_pai};
    private int[] mathCommonValue = {R.string.keyboard_number_one, R.string.keyboard_number_two, R.string.keyboard_number_three, R.string.keyboard_number_four, R.string.keyboard_number_five, R.string.keyboard_number_sex, R.string.keyboard_number_seven, R.string.keyboard_number_eight, R.string.keyboard_number_nine, R.string.keyboard_number_zero, R.string.keyboard_sign_addition, R.string.keyboard_sign_subtraction, R.string.keyboard_sign_multiplication, R.string.keyboard_sign_division, R.string.keyboard_sign_equal, R.string.keyboard_sign_percentage, R.string.keyboard_sign_decimal_point, R.string.keyboard_edit_maohao, R.string.keyboard_edit_ellipsis, R.string.keyboard_sign_approximate, R.string.keyboard_sign_pai};
    /**
     * 数学键盘厘米等单位对应的id和值映射
     **/
    private int[] mathCmKey = {R.id.tv_math_cm_mm, R.id.tv_math_cm_cm, R.id.tv_math_cm_dm, R.id.tv_math_cm_m, R.id.tv_math_cm_km, R.id.tv_math_cm_mg, R.id.tv_math_cm_g, R.id.tv_math_cm_kg, R.id.tv_math_cm_s, R.id.tv_math_cm_min, R.id.tv_math_cm_h, R.id.tv_math_cm_mm2, R.id.tv_math_cm_cm2, R.id.tv_math_cm_dm2, R.id.tv_math_cm_m2, R.id.tv_math_cm_km2, R.id.tv_math_cm_mm3, R.id.tv_math_cm_cm3, R.id.tv_math_cm_dm3, R.id.tv_math_cm_m3, R.id.tv_math_cm_km3, R.id.tv_math_cm_centigrade};
    private int[] mathCmValue = {R.string.keyboard_unit_mm, R.string.keyboard_unit_cm, R.string.keyboard_unit_dm, R.string.keyboard_unit_m, R.string.keyboard_unit_km, R.string.keyboard_unit_mg, R.string.keyboard_unit_g, R.string.keyboard_unit_kg, R.string.keyboard_unit_s, R.string.keyboard_unit_min, R.string.keyboard_unit_h, R.string.keyboard_unit_mm2, R.string.keyboard_unit_cm2, R.string.keyboard_unit_dm2, R.string.keyboard_unit_m2, R.string.keyboard_unit_km2, R.string.keyboard_unit_mm3, R.string.keyboard_unit_cm3, R.string.keyboard_unit_dm3, R.string.keyboard_unit_m3, R.string.keyboard_unit_km3, R.string.keyboard_unit_centigrade};
    /**
     * 数学键盘角度等单位对应的id和值映射
     **/
    private int[] mathCornerKey = {
            R.id.tv_math_corner_zero_power,
            R.id.tv_math_corner_two_power,
            R.id.tv_math_corner_three_power,
            R.id.tv_math_corner_pai,
            R.id.tv_math_corner_thousand,
            R.id.tv_math_corner_corner,
            R.id.tv_math_corner_vertical,
            R.id.tv_math_corner_resemble,
            R.id.tv_math_corner_congruent,
            R.id.tv_math_corner_triangle,
            R.id.tv_math_corner_prallel,
            R.id.tv_math_corner_because,
            R.id.tv_math_corner_so};
    private int[] mathCornerValue = {
            R.string.keyboard_corner_zero_power,
            R.string.keyboard_corner_two_power,
            R.string.keyboard_corner_three_power,
            R.string.keyboard_corner_pai,
            R.string.keyboard_corner_thousand,
            R.string.keyboard_corner_corner,
            R.string.keyboard_corner_vertical,
            R.string.keyboard_corner_resemble,
            R.string.keyboard_corner_congruent,
            R.string.keyboard_corner_triangle,
            R.string.keyboard_corner_prallel,
            R.string.keyboard_corner_because,
            R.string.keyboard_corner_so};

    public KeyBoardMathView(Context context) {
        super(context);
        initKeyBoardMathView();
        initMathKeyboardMap();
        initMathKeyboardCmOnClick();
        initMathKeyboardNumberOnClick();
        initMathKeyboardLetterOnClick();
        initMathKeyboardCornerOnClick();
        initMathKeyboardDivisionOnClick();
    }

    /**
     * 初始化基本数学键盘模型(数学中的字母,数组,单位,符号等)
     **/
    private void initKeyBoardMathView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.keyboard_math, null);
        addView(view, getKeyBoardContentParams());
        mathCmView = LayoutInflater.from(getContext()).inflate(R.layout.keyboard_math_cm, null);
        mathCornerView = LayoutInflater.from(getContext()).inflate(R.layout.keyboard_math_corner, null);
        mathNumberView = LayoutInflater.from(getContext()).inflate(R.layout.keyboard_math_number, null);
        mathLetterSmallView = LayoutInflater.from(getContext()).inflate(R.layout.keyboard_math_letter_small, null);
        mathLetterCaseView = LayoutInflater.from(getContext()).inflate(R.layout.keyboard_math_letter_case, null);
        mathDivisionView = LayoutInflater.from(getContext()).inflate(R.layout.keyboard_math_division, null);
        LinearLayout layout_change = (LinearLayout) findViewById(R.id.layout_change);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layout_change.addView(mathNumberView, layoutParams);
        layout_change.addView(mathDivisionView, layoutParams);
        layout_change.addView(mathLetterSmallView, layoutParams);
        layout_change.addView(mathLetterCaseView, layoutParams);
        layout_change.addView(mathCmView, layoutParams);
        layout_change.addView(mathCornerView, layoutParams);
    }

    /**
     * 初始化所有数学键盘的键值对
     **/
    private void initMathKeyboardMap() {
        keyboardMathNumberMap = new HashMap<>();
        //数学数字键盘键值对
        for (int i = 0; i < mathCommonValue.length; i++) {
            keyboardMathNumberMap.put(mathCommonKey[i], getResources().getString(mathCommonValue[i]));
        }
        //数学乘除键盘键值对
        for (int i = 0; i < mathDivisionValue.length; i++) {
            keyboardMathNumberMap.put(mathDivisionKey[i], getResources().getString(mathDivisionValue[i]));
        }
        //数学键盘字母键值对
        for (int i = 0; i < mathLetterSmallValue.length; i++) {
            keyboardMathNumberMap.put(mathLetterSmallKey[i], getResources().getString(mathLetterSmallValue[i]));
            keyboardMathNumberMap.put(mathLetterCaseKey[i], getResources().getString(mathLetterCaseValue[i]));
        }
        //数学键盘厘米键值对
        for (int i = 0; i < mathCmKey.length; i++) {
            keyboardMathNumberMap.put(mathCmKey[i], getResources().getString(mathCmValue[i]));
        }
        for (int i = 0; i < mathCornerKey.length; i++) {
            keyboardMathNumberMap.put(mathCornerKey[i], getResources().getString(mathCornerValue[i]));
        }
    }

    /**
     * 初始化数字按键点击事件
     **/
    private void initMathKeyboardNumberOnClick() {
        initmathKeyboardCommonOnClick(mathNumberView);
    }

    /**
     * 初始化乘除按键点击事件
     **/
    private void initMathKeyboardDivisionOnClick() {
        for (int aMathDivisionKey : mathDivisionKey) {
            findViewById(aMathDivisionKey).setOnClickListener(keyboardMathOnClick);
        }
        initmathKeyboardCommonOnClick(mathDivisionView);
    }

    /**
     * 初始化字母按键点击事件
     **/
    private void initMathKeyboardLetterOnClick() {
        for (int i = 0; i < mathLetterSmallKey.length; i++) {
            mathLetterSmallView.findViewById(mathLetterSmallKey[i]).setOnClickListener(keyboardMathOnClick);
            mathLetterCaseView.findViewById(mathLetterCaseKey[i]).setOnClickListener(keyboardMathOnClick);
        }
        mathLetterSmallView.findViewById(R.id.tv_keyboard_delete).setOnClickListener(keyboardMathOnClick);
        mathLetterSmallView.findViewById(R.id.tv_keyboard_small).setOnClickListener(keyboardMathNavigationOnClick);
        mathLetterCaseView.findViewById(R.id.tv_keyboard_case).setOnClickListener(keyboardMathNavigationOnClick);
    }

    /**
     * 初始化厘米按键点击事件
     **/
    private void initMathKeyboardCmOnClick() {
        for (int aMathCmKey : mathCmKey) {
            mathCmView.findViewById(aMathCmKey).setOnClickListener(keyboardMathOnClick);
        }
    }

    /**
     * 初始化角度按键点击事件
     **/
    private void initMathKeyboardCornerOnClick() {
        for (int aMathCornerKey : mathCornerKey) {
            mathCornerView.findViewById(aMathCornerKey).setOnClickListener(keyboardMathOnClick);
        }
    }

    /**
     * 初始化公共按键点击事件
     **/
    private void initmathKeyboardCommonOnClick(View commonView) {
        for (int aMathCommonKey : mathCommonKey) {
            commonView.findViewById(aMathCommonKey).setOnClickListener(keyboardMathOnClick);
        }
        commonView.findViewById(R.id.tv_keyboard_delete).setOnClickListener(keyboardMathOnClick);
        commonView.findViewById(R.id.tv_keyboard_delete).setOnLongClickListener(keyboardMathonLongClick);
    }

    /**
     * 显示指定的数学键盘类型
     */
    public void showkeyBoardMathView(View view) {
        mathNumberView.setVisibility(View.GONE);
        mathLetterSmallView.setVisibility(View.GONE);
        mathLetterCaseView.setVisibility(View.GONE);
        mathCmView.setVisibility(View.GONE);
        mathNumberView.setVisibility(View.GONE);
        mathDivisionView.setVisibility(View.GONE);
        view.setVisibility(View.VISIBLE);
    }

    /**
     * 公共按键(删除,每个按键的事件)数学键盘点击事件
     **/
    View.OnClickListener keyboardMathOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (keyBoardCallBack != null) {
                switch (v.getId()) {
                    case R.id.tv_keyboard_delete:
                        keyBoardCallBack.deleteKeyBoard();
                        break;
                    case R.id.tv_keyboard_molecular:
                        keyBoardCallBack.showMolecular();
                        break;
                    default:
                        String mathNumberValue = keyboardMathNumberMap.get(v.getId());
                        keyBoardCallBack.getFinishEditValue(mathNumberValue);
                }
            }
        }
    };

    /**
     * 公共按键(长按删除)数学键盘长按事件
     **/
    View.OnLongClickListener keyboardMathonLongClick = new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            switch (v.getId()) {
                case R.id.tv_keyboard_delete:
                    if (keyBoardCallBack != null) {
                        keyBoardCallBack.deleteLongKeyBoard();
                    }
                    break;
            }
            return false;
        }
    };

    /**
     * 数学五种小键盘(数字,字母,单位,符号,角度)点击绑定事件 对外提供给大键盘的导航使用
     **/
    View.OnClickListener keyboardMathNavigationOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_math_cm:
                    showkeyBoardMathView(mathCmView);
                    break;
                case R.id.tv_math_corner:
                    showkeyBoardMathView(mathCornerView);
                    break;
                case R.id.tv_keyboard_small:
                    showkeyBoardMathView(mathLetterCaseView);
                    break;
                case R.id.tv_math_letter:
                    showkeyBoardMathView(mathLetterSmallView);
                    break;
                case R.id.tv_math_multiplication_division:
                    showkeyBoardMathView(mathDivisionView);
                    break;
                case R.id.tv_math_number:
                    showkeyBoardMathView(mathNumberView);
                    break;
                case R.id.tv_keyboard_case:
                    showkeyBoardMathView(mathLetterSmallView);
                    break;
            }
        }
    };

    /**
     * 设置数学按键的绑定事件
     **/
    public void setOnClickKeyFinishListener(KeyBoardCallBack keyBoardCallBack) {
        this.keyBoardCallBack = keyBoardCallBack;
    }

    /**
     * 内容区域需要的布局参数
     **/
    public RelativeLayout.LayoutParams getKeyBoardContentParams() {
        return new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    }

}
